﻿using System;
using System.Windows.Forms;
using DesignerHelper.EventHandlers;
using DesignerHelper.Main;
using SpreadsheetGear;
using SpreadsheetGear.Commands;
using WorkbookDesigner.Main;
using SGForm = SpreadsheetGear.Windows.Forms;


namespace WorkbookDesigner.UI
{
    internal partial class PasteSpecialDialog : Form
    {
        private static PasteSpecialDialog _psd = null;
        private IMainWindow Mw { get; set; }
        private SGForm.WorkbookView ActiveView { get; set; }


        internal static void ShowForm(IMainWindow form)
        {
            if (_psd == null)
            {
                _psd = new PasteSpecialDialog(form);
                _psd.Show(form.GetForm());
            }
            else
                _psd?.Activate();
        }

        private PasteSpecialDialog()
        {
            InitializeComponent();
        }

        private PasteSpecialDialog(IMainWindow mw) : this()
        {
            Mw = mw;
            ActiveView = mw.ActiveView;
        }
        //事件处理
        //
        private void AddEventHandler()
        {
            Mw.FileOperator.CellBeginEditInternal += DisableDialog;
            Mw.FileOperator.CellCancelEditInternal += EnableDialog;
            Mw.FileOperator.CellEndEditInternal += EnableDialog;
            Mw.FileOperator.DisableUI += DisableDialog;
            Mw.FileOperator.EnableUI += EnableDialog;
        }
        private void RemoveEventHandler()
        {
            Mw.FileOperator.CellBeginEditInternal -= DisableDialog;
            Mw.FileOperator.CellCancelEditInternal -= EnableDialog;
            Mw.FileOperator.CellEndEditInternal -= EnableDialog;
            Mw.FileOperator.DisableUI -= DisableDialog;
            Mw.FileOperator.EnableUI -= EnableDialog;
        }

        private void DisableDialog(object sender, EventArgs e)
        {
            if (e is NoFormLeftEventArgs || e is SGForm.CellBeginEditEventArgs)
                _disableDialog();
        }
        private void EnableDialog(object sender, EventArgs e)
        {
            if (e is FormExistEventArgs || e is SGForm.CellCancelEditEventArgs || e is SGForm.CellEndEditEventArgs)
                _enableDialog();
        }
        private void _enableDialog()
        {
            groupBox1.Enabled = true;
            groupBox2.Enabled = true;
            buttonDoPaste.Enabled = true;
            buttonPasteOK.Enabled = true;
        }
        private void _disableDialog()
        {
            groupBox1.Enabled = false;
            groupBox2.Enabled = false;
            buttonDoPaste.Enabled = false;
            buttonPasteOK.Enabled = false;
        }

        //工作表刷新
        //
        private bool RefreshSuccessed
        {
            get
            {
                RefreshWbv();
                return ActiveView != null;
            }
        }
        private void RefreshWbv()
        {
            ActiveView = Mw.ActiveView;
        }

        //窗体功能主体
        //
        private void Paste()
        {
            if (!RefreshSuccessed)
                return;
            var tempWbv = ActiveView;   
            tempWbv.GetLock();
            try
            {

                CommandRange command = new CommandRange.PasteSpecial(tempWbv.RangeSelection, SelectedPasteType, SelectedPasteOperation, SkipBlank, Transpose);
                tempWbv.ActiveCommandManager.Execute(command);
            }
            finally
            {
                tempWbv.ReleaseLock();
            }
        }
        private PasteOperation SelectedPasteOperation
        {
            get
            {
                switch (comboBoxOperation.SelectedIndex)
                {
                    case 0:
                        return PasteOperation.None;
                    case 1:
                        return PasteOperation.Add;
                    case 2:
                        return PasteOperation.Subtract;
                    case 3:
                        return PasteOperation.Multiply;
                    case 4:
                        return PasteOperation.Divide;
                }
                return PasteOperation.None;
            }
        }
        private PasteType SelectedPasteType
        {
            get
            {
                if (radioPasteAll.Checked)
                    return PasteType.All;

                if (radioPasteExcepetBorder.Checked)
                    return PasteType.AllExceptBorders;

                if (radioPasteColWidth.Checked)
                    return PasteType.ColumnWidths;

                if (radioPasteComments.Checked)
                    return PasteType.Comments;

                if (radioPasteFormulasAndNumFormat.Checked)
                    return PasteType.FormulasAndNumberFormats;

                if (radioPasteFormulas.Checked)
                    return PasteType.Formulas;

                if (radioPasteValidation.Checked)
                    return PasteType.Validation;

                if (radioPasteValueAndNumFormat.Checked)
                    return PasteType.ValuesAndNumberFormats;

                if (radioPasteValues.Checked)
                    return PasteType.Values;

                if (radioPasteFormats.Checked)
                    return PasteType.Formats;

                return PasteType.All;
            }

        }
        private bool SkipBlank => checkBoxSkipBlankCell.Checked;

        private bool Transpose => checkBoxTransCells.Checked;

        //按钮及窗体事件
        //
        private void PasteOK_Click(object sender, EventArgs e)
        {
            Paste();
            Close();
        }
        private void Paste_Click(object sender, EventArgs e)
        {
            Paste();
        }
        private void PasteClose_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void PasteSpecialForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            _psd = null;
            RemoveEventHandler();
        }
        private void PasteSpecialDialog_Load(object sender, EventArgs e)
        {
            comboBoxOperation.SelectedIndex = 0;
            AddEventHandler();
        }

    }

}
